import {Route,Redirect} from 'dva/router';
import dynamic from 'dva/dynamic';
export function renderRoutes_back(routes=[]){
  return (
    routes.map((item,index)=>(
        <Route key={index} path={item.path} render={
          (props) =><item.component {...props} children={item.children}></item.component>
        }/>
    ))
  )
}

export function renderRoutes(routes=[],app){
  return (
    routes.map((item,index)=>(
        <Route key={index} path={item.path} component={
          dynamic({
            app,
            component:()=>{
                if(item.needLogin && !localStorage.getItem('token')){
                  return ()=><Redirect to="login"/>
                }
                return  item.component().then(response=>{
                    let Component = response.default || response;
                    return props=><Component {...props} children={item.children} app={app}/>
               })
            },
          })
        }/>
    ))
  )
}